package com.hanter.demo.raddemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.hanter.android.radlib.bitmap.BitmapUtils;
import com.hanter.android.radlib.saf.FileManager;
import com.hanter.android.radlib.util.FileUtils;
import com.hanter.android.radui.mvp.MvpTitleBarActivity;
import com.hanter.demo.raddemo.test.TestBannerActivity;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends MvpTitleBarActivity<MainContract.View, MainPresenter>
        implements MainContract.View {

    @Override
    public MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("测试");

        setTitleBarVisibility(View.VISIBLE);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1: {
                //
                if (resultCode == Activity.RESULT_OK) {
                    Toast.makeText(this, "拍照", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btnMvvm: {
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                startActivity(intent);
            }
            break;

            case R.id.btnTabBar: {
                Intent intent = new Intent(MainActivity.this, TabBarActivity.class);
                startActivity(intent);
            }
            break;

            case R.id.btnCupertinoDialog: {
                Intent intent = new Intent(MainActivity.this, CupertinoDialogActivity.class);
                startActivity(intent);
            }
            break;

            case R.id.btnTabLayout: {
                Intent intent = new Intent(MainActivity.this, LazyFragmentActivity.class);
                startActivity(intent);
            }
            break;


            case R.id.btnBanner: {
//                Intent intent = new Intent(MainActivity.this,BannerActivity.class);
//                startActivity(intent);
                Intent intent = new Intent(MainActivity.this, TestBannerActivity.class);
                startActivity(intent);
            }
            break;

            case R.id.btnTitleBar: {
//                Intent intent = new Intent(MainActivity.this, TitleBarActivity.class);
//                startActivity(intent);

                BitmapUtils.setBitmapDefaultDensity(160);
            }
            break;

            case R.id.btnNewFile: {
                try {
                    for (int i = 0; i < 10; i++) {
                        File file = FileUtils.createFile(getFilesDir(), "Test", ".tmp");
                        Log.e("Test", "file name: " + file.getAbsolutePath());
                        Thread.sleep(100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            break;

            case R.id.btnNewMediaFile: {

                FileManager fm = new FileManager(this);

                try {

                    Uri uri;

//                    uri = fm.createMediaUri(Environment.DIRECTORY_PICTURES, "test" + Environment.DIRECTORY_PICTURES, "image/jpeg");
//                    takePhoto(uri);

                    uri = fm.createMediaUri(Environment.DIRECTORY_MUSIC, "test" + Environment.DIRECTORY_MUSIC, "audio/x-wav");
                    testWrite(uri);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                /*
                uri = fm.createMediaUri(Environment.DIRECTORY_MUSIC, "test" + Environment.DIRECTORY_MUSIC, "audio/x-wav");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_PODCASTS, "test" + Environment.DIRECTORY_PODCASTS, "audio/x-wav");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_RINGTONES, "test" + Environment.DIRECTORY_RINGTONES, "audio/x-wav");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_NOTIFICATIONS, "test" + Environment.DIRECTORY_NOTIFICATIONS, "audio/x-wav");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_ALARMS, "test" + Environment.DIRECTORY_ALARMS, "audio/x-wav");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_PICTURES, "test" + Environment.DIRECTORY_PICTURES, "image/jpeg");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_MOVIES, "test" + Environment.DIRECTORY_MOVIES, "video/mpeg");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_DOWNLOADS, "test" + Environment.DIRECTORY_DOWNLOADS, "image/jpeg");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_DCIM, "test" + Environment.DIRECTORY_DCIM, "image/jpeg");
                testWrite(uri);

                uri = fm.createMediaUri(Environment.DIRECTORY_DOCUMENTS, "test" + Environment.DIRECTORY_DOCUMENTS, "application/vnd.ms-excel");
                testWrite(uri);


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    uri = fm.createMediaUri(Environment.DIRECTORY_AUDIOBOOKS, "test" + Environment.DIRECTORY_AUDIOBOOKS, "audio/x-wav");
                    testWrite(uri);

                    uri = fm.createMediaUri(Environment.DIRECTORY_SCREENSHOTS, "test" + Environment.DIRECTORY_SCREENSHOTS, "image/jpeg");
                    testWrite(uri);
                }
                */

                /*
                List<String> list = new ArrayList<>();
                list.add("Test");
                boolean success = ValidatorWrapper.Companion.of("test")
                        .notEmpty("test")
                        .min(4, "test")
                        .of(list)
                        .notEmpty("字符数组不能为空")
                        .ifExists(true)
                        .of("test")
                        .min(10, "最短10")
                        .of(20)
                        .max(18, "测试大于")
                        .orElse()
                        .min(21, "测试小于")
                        .end()
                        .validate();

                if (!success) {

                }

                 */
                break;
            }

            default:
                super.onClick(v);
        }

    }

    private void testWrite(Uri uri) {
        if (uri == null) return;

        try (OutputStream is = getContentResolver().openOutputStream(uri, "rw")) {
            if (is != null) {
                is.write(new byte[] {1, 2, 3, 4, 5});
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void takePhoto(Uri uri) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, 1);
    }

}
